package org.testis.webautomationmodule.ui;

import org.testis.utils.StringVerification;
import org.testis.logging.AtofLogLevel;

/**
 * Defines a form field decorator. UITextbox is
 * a ui form field
 * @author nurhat
 */
public class UIFormFieldDecorator extends WebAutomationDecorator {

    /**
     * Declares id of a form field
     */
    protected String m_id;
    protected String m_value;
    private boolean m_isDynamic;

    public UIFormFieldDecorator(String id) {
        setId(id);
        setIsDynamic(false);

    }

    public UIFormFieldDecorator(String id,
            String value) {
        setId(id);
        if (value != null) {
            setValue(value);
        }
        setIsDynamic(false);
    }

    public UIFormFieldDecorator(String id,
            String value, boolean isDynamic) {
        setId(id);
        setValue(value);
        setIsDynamic(isDynamic);
    }

    @Override
    public boolean getBehavior() {
        String val = "";
        if (checkAvailableForVerification()) {
            val = getWebAutomationImpl().getValue(getId());
            setValue(val);
            putLog("Gets value of object with id="
                    + getId() + " value=" + getValue());
            return true;
        } else {
            putLog("Could not get value of object with id="
                    + getId(), AtofLogLevel.ERROR);
            return false;
        }

    }

    @Override
    public boolean verifyBehavior() {
        String val = "";
        if (checkAvailableForVerification()) {
            val = getWebAutomationImpl().getValue(getId());

            boolean result =
                    StringVerification.verifyText(getValue(),
                    val);
            if (result) {
                putLog(" Object with " + getId()
                        + " id was  verified... curValue:"
                        + val
                        + " compared with:"
                        + getValue());
                setValue(val);
                return true;
            } else {
                putLog(" Object with " + getId()
                        + " id was  not verified.... curValue:"
                        + val
                        + " compared with:"
                        + getValue(), AtofLogLevel.ERROR);
                setValue(val);
            }
        } else {
            putLog(" Object with " + getId()
                    + " id was not available for verification...", AtofLogLevel.ERROR);
            setValue(null);
        }
        return false;
    }

    /**
     * Checks whether  form field is editable or not
     * @return
     */
    public boolean checkAvailableForAction() throws UIElementNotFoundException,
            UIElementNotEditableException {
        if (isElementPresent()) {
            String objText = getWebAutomationImpl().getText(getId());
            if (isVisible()) {
                if (isEditable()) {
                    return true;
                } else {
                    putLog(" Object with (" + objText + ")" + getId()
                            + " id is not editable ", AtofLogLevel.ERROR);
                    throw new UIElementNotEditableException(
                            " Object with (" + objText + ")" + getId()
                            + " id is not editable ");
                }
            } else {
                putLog(" Object with " + getId() + " id is not visible...", AtofLogLevel.ERROR);
                throw new UIElementNotVisibleException(
                        " Object with " + getId() + " id is not present...");
            }
        } else {

            putLog(" Object with " + getId() + " id is not present...", AtofLogLevel.ERROR);
            throw new UIElementNotFoundException(
                    " Object with " + getId() + " id is not present...");
        }

    }

    /**
     * Checks whether  form field is verifiable or not 
     * @return
     */
    public boolean checkAvailableForVerification() {
        if (isElementPresent()) {
            if (isVisible()) {
                return true;
            } else {
                putLog(" Object with " + getId() + " id is not visible...", AtofLogLevel.WARN);
                return true;
            }
        } else {
            putLog(" Object with " + getId()
                    + " id is not present...", AtofLogLevel.ERROR);
        }
        return false;
    }

    /**
     * Returns attribute of ui object as string according to given parameter
     * e.g. getAttribute("onclick") return onclick attribute of ui form field.
     * @param attributeName
     * @return
     */
    public String getAttribute(String attributeName) {
        return getWebAutomationImpl().getAttribute(getId() + "@" + attributeName);
    }

    public Number getElementIndex() {
        return getWebAutomationImpl().getElementIndex(getId());
    }

    public String getId() {
        return m_id;
    }

    public boolean isEditable() {
        return getWebAutomationImpl().isEditable(getId());
    }

    public boolean isElementPresent() {
        return getWebAutomationImpl().isElementPresent(getId());
    }

    public boolean isVisible() {
        return getWebAutomationImpl().isVisible(getId());
    }

    public void setId(String id) {
        if (id == null) {
            id = "";
        }
        this.m_id = id;
    }

    public String getValue() {
        return m_value;
    }

    public void setValue(String val) {
        this.m_value = val.trim();
    }

    /**
     * @return the m_isDynamic
     */
    public boolean isDynamic() {
        return m_isDynamic;
    }

    /**
     * @param m_isDynamic the m_isDynamic to set
     */
    public void setIsDynamic(boolean isDynamic) {
        this.m_isDynamic = isDynamic;
    }
}
